﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
//using CustomControls;

namespace Nivdal
{

    public partial class AdminApproveUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                OnlineUser ou = (OnlineUser)Session["User"];
                //String UserUpdateConfirmMessage = Request.QueryString["ConfirmNote"];
                AccessLevelHiddenField.Value = ou.UserAccessLevel;
                if (ou.UserAccessLevel != ("5"))
                {
                    Response.Redirect("~/Default.aspx");
                }
                this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AdminApproveUsers_PageTitle;
            }
        }


        //protected string ApproveUser(string UserId)
        //{
        //    //Response.Write(UserId);
        //    string returnValue = "ok";
        //    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    System.Data.SqlClient.SqlConnection
        //    strConnection = new System.Data.SqlClient.SqlConnection(conn);
        //    string StrSQL = "Nivdal_UserApprovalApproveUser";
        //    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
        //    cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));
        //    strConnection.Open();
        //    try
        //    {
        //        cmd.ExecuteReader();

        //    }
        //    catch (Exception SQLDBException)
        //    {
        //        returnValue = SQLDBException.ToString();
        //        return returnValue;
        //        //Response.Write(SQLDBException.ToString());

        //    }

        //    finally
        //    {
        //        strConnection.Close();
        //    }


        //    return returnValue;
        //}


        //protected string DeclineUser(string UserId)
        //{
        //    //Response.Write(UserId);
        //    string returnValue = "ok";
        //    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    System.Data.SqlClient.SqlConnection
        //    strConnection = new System.Data.SqlClient.SqlConnection(conn);
        //    string StrSQL = "Nivdal_UserApprovalDeclineUser";
        //    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
        //    cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));
        //    strConnection.Open();
        //    try
        //    {
        //        cmd.ExecuteReader();

        //    }
        //    catch (Exception SQLDBException)
        //    {
        //        returnValue = SQLDBException.ToString();
        //        return returnValue;
        //        //Response.Write(SQLDBException.ToString());

        //    }

        //    finally
        //    {
        //        strConnection.Close();
        //    }


        //    return returnValue;
        //}

        //protected void UpdateNewUser(Object sender, GridViewCommandEventArgs e)
        //{

        //    if (e.CommandName == "ApproveUser")
        //    {
        //        // get the row index stored in the CommandArgument property
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        // get the GridViewRow where the command is raised
        //        GridViewRow row = ApprovalUsersGridView.Rows[index];
        //        string ReturnValue = "";
        //        string UserName = row.Cells[1].Text;
        //        string UserId = row.Cells[0].Text;
        //        ReturnValue = ApproveUser(UserId);
        //        if (ReturnValue == "ok")
        //        {
        //            Response.Redirect("AdminApproveUsers.aspx?ConfirmNote=" + " " + Resources.Resource.AdminApproveUsers_TheUser + " " + UserName + Resources.Resource.Global_HasbeenApproved);
        //        }

        //        else
        //            ApproveActionLabel.Text = ReturnValue;

        //    }
        //    if (e.CommandName == "DeclineUser")
        //    {
        //        // get the row index stored in the CommandArgument property
        //        int index = Convert.ToInt32(e.CommandArgument);
        //        // get the GridViewRow where the command is raised
        //        GridViewRow row = ApprovalUsersGridView.Rows[index];
        //        string ReturnValue = "";
        //        string UserName = row.Cells[1].Text;
        //        string UserId = row.Cells[0].Text;
        //        ReturnValue = DeclineUser(UserId);
        //        if (ReturnValue == "ok")
        //        {
        //            Response.Redirect("AdminApproveUsers.aspx?ConfirmNote=" +  Resources.Resource.AdminApproveUsers_TheUser + " " + UserName + " " + Resources.Resource.AdminApproveUsers_HasBeenDeclined);
        //        }

        //        else
        //            ApproveActionLabel.Text = ReturnValue;

        //    }

        //}

        //protected void ApprovalUsersGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        //{
        //    e.Row.Cells[0].Visible = false;
        //}

        protected void UnlockUser(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Unlock")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                GND.ExecNivdalSQL("UserId", "Nivdal_UnlockUser", e.CommandArgument);
                Response.Redirect("AdminApproveUsers.aspx");
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        
        protected void UpdateNewUser(Object sender, GridViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string Answer = "";
            if (e.CommandName == "ApproveUser")
            {
                Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalApproveUser", e.CommandArgument);
                GND.SendPendingMails();
            }
            if (e.CommandName == "DeclineUser")
            {
                Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalDeclineUser", e.CommandArgument);
            }

            AccessLevelLabel.Text = Answer;
            //AccessLevelLabel.Visible = true;
            ApprovalUsersGridView.DataBind();
            //UsersToApproveSqlDataSource.DataBind();
            //ApprovalUsersGridView.DataBind();
        }

        protected string IPCountryString(object IPAddress)
        {
            if(IPAddress.ToString().Length > 5)
                return GetNivdalData.GetNivdalData.GetIPCountrystring(IPAddress.ToString());
            return "";
        }

    }
}